<template>
  <div class="dashboard-container">
    <el-container class="layout-container">
      <!-- Sidebar Navigation -->
      <el-aside width="260px" class="app-sidebar">
        <div class="sidebar-header">
          <div class="logo-icon manager-icon">
            <el-icon><Management /></el-icon>
          </div>
          <span class="app-name">TeamPlan360</span>
        </div>
        
        <el-menu
          :default-active="activeTab"
          class="sidebar-menu"
          @select="handleMenuSelect"
          background-color="transparent"
          text-color="#6b7280"
          active-text-color="#4f46e5"
        >
          <el-menu-item index="overview">
            <el-icon><DataBoard /></el-icon>
            <span>团队概览</span>
          </el-menu-item>
          <el-menu-item index="employees">
            <el-icon><UserFilled /></el-icon>
            <span>员工管理</span>
          </el-menu-item>
          <el-menu-item index="progress">
            <el-icon><TrendCharts /></el-icon>
            <span>进度监控</span>
          </el-menu-item>
          <el-menu-item index="summary">
            <el-icon><Trophy /></el-icon>
            <span>年度总结</span>
          </el-menu-item>
          <el-menu-item index="reports">
            <el-icon><Document /></el-icon>
            <span>部门报告</span>
          </el-menu-item>
        </el-menu>
        
        <div class="sidebar-footer">
          <div class="user-profile">
            <el-avatar :size="36" class="user-avatar manager-avatar">{{ user?.username?.charAt(0).toUpperCase() }}</el-avatar>
            <div class="user-info">
              <span class="username">{{ user?.username }}</span>
              <span class="role">部门经理</span>
            </div>
            <el-button type="text" class="logout-btn" @click="showPasswordDialog = true" title="修改密码">
              <el-icon><Lock /></el-icon>
            </el-button>
            <el-button type="text" class="logout-btn" @click="handleLogout" title="退出登录">
              <el-icon><SwitchButton /></el-icon>
            </el-button>
          </div>
        </div>
      </el-aside>

      <!-- Main Content -->
      <el-container>
        <el-header class="app-header">
          <div class="header-left">
            <h2 class="page-title">{{ getPageTitle(activeTab) }}</h2>
          </div>
          <div class="header-right">
            <el-tag type="warning" effect="dark" round>管理模式</el-tag>
          </div>
        </el-header>

        <el-main class="app-main">
          <div class="content-wrapper">
            <transition name="fade" mode="out-in">
              <component :is="currentComponent" />
            </transition>
          </div>
        </el-main>
      </el-container>
    </el-container>
    <ChangePasswordDialog v-model="showPasswordDialog" />
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import TeamOverview from '../components/TeamOverview.vue'
import EmployeeManagement from '../components/EmployeeManagement.vue'
import ProgressMonitoring from '../components/ProgressMonitoring.vue'
import AnnualSummary from '../components/AnnualSummary.vue'
import DepartmentReports from '../components/DepartmentReports.vue'
import ChangePasswordDialog from '../components/ChangePasswordDialog.vue'
import { 
  Management, DataBoard, UserFilled, TrendCharts, Trophy, SwitchButton, Document, Lock 
} from '@element-plus/icons-vue'

export default {
  name: 'ManagerDashboard',
  components: {
    TeamOverview,
    EmployeeManagement,
    ProgressMonitoring,
    AnnualSummary,
    DepartmentReports,
    ChangePasswordDialog,
    Management, DataBoard, UserFilled, TrendCharts, Trophy, SwitchButton, Document, Lock
  },
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const activeTab = ref('overview')
    const showPasswordDialog = ref(false)
    const user = computed(() => authStore.user)
    
    const handleMenuSelect = (index) => {
      activeTab.value = index
    }

    const handleLogout = () => {
      authStore.logout()
      router.push('/login')
    }
    
    const getPageTitle = (tab) => {
      const titles = {
        overview: '团队工作概览',
        employees: '员工账号管理',
        progress: '项目进度监控',
        summary: '年度工作总结'
      }
      return titles[tab] || '管理工作台'
    }

    const currentComponent = computed(() => {
      const map = {
        overview: TeamOverview,
        employees: EmployeeManagement,
        progress: ProgressMonitoring,
        summary: AnnualSummary,
        reports: DepartmentReports
      }
      return map[activeTab.value]
    })
    
    return {
      activeTab,
      user,
      showPasswordDialog,
      handleMenuSelect,
      handleLogout,
      getPageTitle,
      currentComponent
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  height: 100vh;
  background-color: #f3f4f6;
}

.layout-container {
  height: 100%;
}

/* Sidebar */
.app-sidebar {
  background: white;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  height: 64px;
  display: flex;
  align-items: center;
  padding: 0 24px;
  border-bottom: 1px solid var(--border-color);
}

.logo-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 12px;
}

.manager-icon {
  background: var(--warning-color);
}

.app-name {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text-main);
}

.sidebar-menu {
  border-right: none;
  padding: 16px 0;
  flex: 1;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border-color);
}

.user-profile {
  display: flex;
  align-items: center;
  padding: 8px;
  background: #f9fafb;
  border-radius: 8px;
}

.user-avatar.manager-avatar {
  background: #fef3c7;
  color: #d97706;
  font-weight: 600;
}

.user-info {
  margin-left: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.username {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-main);
}

.role {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.logout-btn {
  color: var(--text-secondary);
}

.logout-btn:hover {
  color: var(--danger-color);
}

/* Main Content */
.app-header {
  background: white;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
  border-bottom: 1px solid var(--border-color);
}

.page-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-main);
}

.app-main {
  padding: 24px 32px;
  overflow-y: auto;
}

.content-wrapper {
  max-width: 1400px;
  margin: 0 auto;
}

/* Transitions */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.2s ease;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>
